import UNI_APP from "../.env.js"
export const globalMethod = (app) => {
	// 应用终端信息
	let terminal = 0
	// #ifdef H5
	terminal = 0
	if (window.java) {
		terminal = 1
		app.config.globalProperties.$java = window.java;
	}
	// #endif
	// #ifdef MP-WEIXIN
	terminal = 3
	// #endif
	app.config.globalProperties.$terminal = terminal;
	// 返回上一级
	app.config.globalProperties.goBack = () => {
		// #ifndef H5
		uni.navigateBack({
			delta: 1,
		})
		// #endif
		// #ifdef H5
		history.back()
		// #endif
	}
	// 获取线上静态资源
	app.config.globalProperties.getResource = (path) => {
		return UNI_APP.FILE_URL + path
	}
	// 跳转页面
	app.config.globalProperties.goPage = (url) => {
		uni.navigateTo({
			url: url
		})
	}
	// 跳转页面
	app.config.globalProperties.hasNext = (total, pageSize, pageNum) => {
		return Math.ceil(total / pageSize) > (pageNum - 1)
	}
	// 转小数
	app.config.globalProperties.fenyuan2 = (price) => {
		return (price / 100.0).toFixed(2);
	}
	// 复制
	app.config.globalProperties.copyText = (text, callBack) => {
		// #ifndef H5 
		uni.setClipboardData({
			data: text.toString(),
			success: () => {
				callBack && callBack() || uni.showToast({
					icon: "none",
					title: '复制成功'
				})
			}
		});
		// #endif
		// #ifdef H5 
		const textarea = document.createElement('textarea');
		textarea.value = text;
		document.body.appendChild(textarea);
		textarea.select();
		const successful = document.execCommand('copy');
		document.body.removeChild(textarea);
		callBack && callBack() || uni.showToast({
			icon: "none",
			title: '复制成功'
		})
		// #endif
	}
}